//
//  BirthdayVC.m
//  jinwang
//
//  Created by jzb on 2018/3/5.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "BirthdayVC.h"
#import "BRPickerView.h"
#import "PGDatePicker.h"

@interface BirthdayVC ()<PGDatePickerDelegate>
{
    
    UILabel *titleLabel;
}
@property (nonatomic,strong) UIButton *preserveBtn;

@property (nonatomic,strong) NSString *birthdayDate;

@end

@implementation BirthdayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"出生年月";
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *preserveItem = [[UIBarButtonItem alloc]initWithCustomView:self.preserveBtn];
    self.navigationItem.rightBarButtonItem = preserveItem;
    
    //    __block typeof(self) weakSelf = self;
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 84, kScreenWidth-60, 30)];
    
    titleLabel.font = [UIFont systemFontOfSize:18];
    
    titleLabel.text = @"设置您的出生年月";
    
    [self.view addSubview:titleLabel];
    
//    titleLabel.userInteractionEnabled = YES;
//    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPre:)];
//    [titleLabel addGestureRecognizer:longPress];
    
    UIImageView *jw_calendar_BG = [[UIImageView alloc]initWithFrame:CGRectMake(20, 120, kScreenWidth - 40, 50)];
    
    jw_calendar_BG.image = [UIImage imageNamed:@"jw_calendar_BG"];
    
    [self.view addSubview:jw_calendar_BG];
    
    UIImageView *calendarIcon = [[UIImageView alloc]initWithFrame:CGRectMake(jw_calendar_BG.width - 43, 10, 27, 30)];
    
    calendarIcon.image = [UIImage imageNamed:@"jw_calendar"];
    
    [jw_calendar_BG addSubview:calendarIcon];
    
    jw_calendar_BG.userInteractionEnabled = YES;
    
    UIButton *calendarTitle = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, jw_calendar_BG.width-53, 30)];
    
//    calendarTitle.backgroundColor = [UIColor orangeColor];
    calendarTitle.titleLabel.font = [UIFont systemFontOfSize:18];
    
    [calendarTitle setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
    
    calendarTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    [calendarTitle addTarget:self action:@selector(pickerDateBuild:) forControlEvents:UIControlEventTouchUpInside];

    [jw_calendar_BG addSubview:calendarTitle];
    
    [self pickerDateBuild:calendarTitle];
}
//// 使label能够成为响应事件，为了能接收到事件（能成为第一响应者）
//- (BOOL)canBecomeFirstResponder{
//    return YES;
//}
//// 可以控制响应的方法
//- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
//    return (action == @selector(copy:));
//}
////针对响应方法的实现，最主要的复制的两句代码
//- (void)copy:(id)sender{
//
//    //UIPasteboard：该类支持写入和读取数据，类似剪贴板
//    UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
//    pasteBoard.string = titleLabel.text;
//}
//// 处理长按事件
//- (void)longPre:(UILongPressGestureRecognizer *)recognizer{
//    [self becomeFirstResponder]; // 用于UIMenuController显示，缺一不可
//
//    //UIMenuController：可以通过这个类实现点击内容，或者长按内容时展示出复制等选择的项，每个选项都是一个UIMenuItem对象
//    UIMenuItem *copyLink = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copy:)];
//    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:copyLink, nil]];
//    [[UIMenuController sharedMenuController] setTargetRect:titleLabel.frame inView:titleLabel.superview];
//    [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
//}

#pragma mark -懒加载
- (UIButton *)preserveBtn{
    if (!_preserveBtn) {
        _preserveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _preserveBtn.frame = CGRectMake(0, 0, 44, 44);
        [_preserveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_preserveBtn addTarget:self action:@selector(preserveButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _preserveBtn;
}

- (void)preserveButOnclick:(UIButton *)sender{
    
    if (kStringIsEmpty(_birthdayDate)) {
        
        [self showBottomMessag:@"请设置您的出生年月"];
        
    }else{
        
        self.birthdayDateBlcok(_birthdayDate);
        [self goBack];
    }
    
}
- (void)pickerDateBuild:(UIButton *)sender{
    
    NSLog(@"asdfasdfasdf");
//    NSString *maxDateStr = [[NSDate date] stringWithFormat:@"yyyy年MM月dd日"];
//    
//    [BRDatePickerView showDatePickerWithTitle:@"" dateType:UIDatePickerModeDate defaultSelValue:@"" minDateStr:@"" maxDateStr:maxDateStr isAutoSelect:YES resultBlock:^(NSString *selectValue) {
//        //                weakSelf.birthdayTF.text = selectValue;
//        [sender setTitle:[self timeWithTimeIntervalString:selectValue] forState:UIControlStateNormal];
//        
//        _birthdayDate = selectValue;
//    }];
    
    PGDatePicker *datePicker = [[PGDatePicker alloc]init];
    
    datePicker.delegate = self;
    
    [datePicker show];
    
//    datePicker.minimumDate = [NSDate setYear:1990 month:1 day:1];
    
//    datePicker.maximumDate = [NSDate date];//当前时间
    
//    datePicker.titleLabel.text = @"请选择考试时间";
    
//    datePicker.headerView.hidden = YES;
    
    datePicker.headerViewHeight = 2;
    
    datePicker.cancelButton.hidden = YES;
    
    datePicker.confirmButton.hidden = YES;
    
    datePicker.datePickerMode = PGDatePickerModeDate;
    
    datePicker.DateBlcok = ^(NSDateComponents *dateComponents){   // 1
        
         [sender setTitle:[NSString stringWithFormat:@"%li年%li月%li日",dateComponents.year,dateComponents.month,dateComponents.day] forState:UIControlStateNormal];
        _birthdayDate = [NSString stringWithFormat:@"%li-%li-%li",dateComponents.year,dateComponents.month,dateComponents.day];

    };
    
}
//时间戳转换成时间
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *date = [dateFormatter dateFromString:timeString];
    
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy年MM月dd日"];
    
    NSString* dateString = [formatter stringFromDate:date];
    
    return dateString;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
